﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
namespace Golf.WebUI.Customers
{
    public partial class AddressDetails : System.Web.UI.Page
    {

        public int sexs;
        public string fristnames;
        public string lastnames;
        public string addresss; //this.street_address.Value;
        public string citys; //this.city.Value;
        public string provinces; //this.state.Value;
        public string email_addresss; //邮编this.email_address.Value;
        public string tels; //this.telephone.Value;
        public string postcodes;
        public string countrys;//国家
        public string provincess;
        /// <summary>
        /// 登录用户的id
        /// </summary>
        public int userid;
        /// <summary>
        /// 偷懒 写个简单类
        /// </summary>
        public class droplist
        {

            public int id { get; set; }
            public string Address { get; set; }

        }
        public Golf.BLL.Golf_Address userdatebll = new BLL.Golf_Address();
        public Golf.BLL.Golf_User userbll = new BLL.Golf_User();
        public Golf.Model.Golf_Address userdate = new Model.Golf_Address();
        public Golf.Model.Golf_User user = new Model.Golf_User();

        /// <summary>
        /// 获取主机名称
        /// </summary>
        public string http_Host = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            http_Host = ThumNail.GetHttpHost();
            Object obj = CookiesHelper.GetCookie("_Customer_id");
            if (obj == null || obj.ToString() == "")
            {
            }
            else
            {
                userid = int.Parse(obj.ToString());
            }
            if (!IsPostBack)
            {
                switch (Request.Form["action"])
                {
                    case "checkout":
                        UpdateMessage();
                        break;
                    case "Delete":
                        DeleteBySelect();
                        break;
                    case "getAddress":
                        getAddress(1);
                        break;
                }
                SelectByID();
            }

        }
        /// <summary>
        /// 获取地址
        /// </summary>
        /// <param name="p"></param>
        private void getAddress(int p)
        {
            Golf.Model.Golf_Address address = userdatebll.GetModel(p);
            if (address == null)
            {
                Response.Write("-1,");
                return;
            }
            else
            {
                //转换相同
                this.sexs = int.Parse(address.LastName);
                this.fristnames = address.ConsigneeFrist;//收货 人 姓
                this.lastnames = address.ConsigneeLast;//收货人名
                this.addresss = address.Address;
                this.postcodes = address.PostCode;
                this.countrys = address.Province;//国家
                this.citys = address.City;//省
                this.provincess = address.Borough;//市
                this.email_addresss = address.Email;
                this.tels = address.Tel;

            }
        }

        #region    根据ID修改填写信息
        /// <summary>
        /// 根据ID修改填写信息
        /// </summary>
        /// <param name="id">状态id</param>
        public void UpdateMessage()
        {
            int sex = int.Parse(Server.UrlDecode(Request.Form["sex"]));
            string fristname = Server.UrlDecode(Request.Form["firstname"]);
            string lastname = Server.UrlDecode(Request.Form["lastname"]);
            string address = Server.UrlDecode(Request.Form["street_address"]);
            //this.street_address.Value;
            string city = Server.UrlDecode(Request.Form["city"]);
            //this.city.Value;
            string province = Server.UrlDecode(Request.Form["state"]);
            //this.state.Value;
            string email_address = Server.UrlDecode(Request.Form["email_address"]);
            //邮编this.email_address.Value;
            string tel = Server.UrlDecode(Request.Form["telephone"]);
            //this.telephone.Value;
            string postcode = Server.UrlDecode(Request.Form["postcode"]);
            string country = Server.UrlDecode(Request.Form["_country"]);
            //国家
            //int number = int.Parse(Server.UrlDecode(Request.Form["number"]));//修改的用户id
            // int address_Id = int.Parse(Server.UrlDecode(Request.Form["addressId"]));//修改的用户  送货地址id

            int selectid = int.Parse(Request.Form["addresstest"]);
            userdate = new Golf.Model.Golf_Address();
            if (selectid != -1)
            {
                userdate = userdatebll.GetModel(selectid);
            }
            //定义性别存储到数据库中，没有字段，替换字段
            userdate.FristName = fristname + " " + lastname;
            userdate.LastName = sex.ToString(); ;
            userdate.ConsigneeFrist = fristname;
            userdate.ConsigneeLast = lastname;
            userdate.Address = address;
            userdate.PostCode = postcode;
            userdate.Province = country;//国家
            userdate.City = city;//省
            userdate.Borough = province;//市
            userdate.Tel = tel;
            userdate.Mobile = tel;
            userdate.Email = email_address;
            userdate.UserId = userid;
            //判断是否是的下拉列表框值
            if (selectid.ToString() == "-1")
            {
                int addressid = userdatebll.Add(userdate);
                if (addressid > 0)
                {
                    Response.Write("Add new address success!,add," + "\t" + userdate.Province + "\t" + userdate.City + "\t" + userdate.Borough + "\t" + userdate.Address + "," + addressid + ",");
                }
                else
                {
                    Response.Write("Add new address failure!,");
                }
            }
            else
            {
                if (userdatebll.Update(userdate))
                {
                    Response.Write("Address update success!,");
                }
                else
                {
                    Response.Write("Address update failure!,");
                }
            }
        }

        #endregion

        #region 根据 ID查找收货人的地址ID

        /// <summary>
        /// 根据 ID查找收货人的地址ID
        /// </summary>
        /// <param name="ID"></param>
        public void SelectByID()
        {
            List<Golf.Model.Golf_Address> list = userdatebll.GetModelList(" UserId=" + userid);

            List<droplist> dlist = new List<droplist>();
            foreach (var item in list)
            {
                userdate = item;
                droplist m = new droplist();
                m.id = item.Id;
                m.Address = "\t" + item.Province + "\t" + item.City + "\t" + item.Borough + "\t" + item.Address;
                dlist.Add(m);
            }
            DropDownList1.DataSource = dlist;
            DropDownList1.DataBind();
            //设置Item的初始项
            ListItem li = new ListItem();
            li.Value = "-1";
            li.Text = "Add NEW Address";
            DropDownList1.Items.Insert(0, li);
        }

        #endregion

        #region  删除下拉列表框中的值
        /// <summary>
        /// 删除下拉列表框中的值
        /// </summary>
        public void DeleteBySelect()
        {
            int selectid = int.Parse(Request.Form["addresstest"]);
            if (selectid.ToString() != "")
            {
                //调用删除数据
                if (userdatebll.Delete(selectid))
                {
                    Response.Write("Delete address success!,0,");
                }
                else
                {
                    Response.Write("Delete address failure!,1,");
                }
            }
            else
            {
                Response.Write("Error,");
            }
        }
        #endregion
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            userdate = userdatebll.GetModel(int.Parse(DropDownList1.SelectedValue));
            delete.Enabled = false;
            if (userdate == null) return;
            delete.Enabled = true;
            //转换相同
            this.sexs = int.Parse(userdate.LastName);
            this.fristnames = userdate.ConsigneeFrist;//收货 人 姓
            this.lastnames = userdate.ConsigneeLast;//收货人名
            this.addresss = userdate.Address;
            this.postcodes = userdate.PostCode;
            this.countrys = userdate.Province;//国家
            this.citys = userdate.City;//省
            this.provincess = userdate.Borough;//市
            this.email_addresss = userdate.Email;
            this.tels = userdate.Tel;
            // UpdatePanel1.Update();
        }
    }
}